package com.yiqi.lottery.feature.home.view;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.utils.T;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.util.NoFastClickUtils;
import com.yiqi.lottery.entity.bo.OrderBo;
import com.yiqi.lottery.entity.bo.TokenBo;
import com.yiqi.lottery.entity.enums.CreateOrderType;
import com.yiqi.lottery.feature.home.contract.PayContract;
import com.yiqi.lottery.feature.home.dependencies.DaggerPayComponent;
import com.yiqi.lottery.feature.home.dependencies.PayPresenterModule;
import com.yiqi.lottery.feature.lottery.dependencies.hall.DaggerHallComponent;
import com.yiqi.lottery.feature.lottery.dependencies.hall.HallPresenterModule;
import com.yiqi.lottery.feature.mine.contract.MineContract;
import com.yiqi.lottery.feature.mine.dependencies.mine.DaggerMineComponent;
import com.yiqi.lottery.feature.mine.dependencies.mine.MinePresenterModule;
import com.yiqi.lottery.feature.mine.view.OrderActivity;
import com.yiqi.lottery.feature.mine.view.RechargeActivity;

import javax.inject.Inject;

public class PayActivity extends Activity implements PayContract.View {


    @Inject

    PayContract.Presenter mPresenter;

    @BindView(R.id.tv_pay_prize)
    TextView mTvPayPrize;
    @BindView(R.id.tv_pay_banlance)
    TextView mTvPayBanlance;

    ProgressDialog progressDialog;

    String mLottoData = "", mBetList = "";
    String mIssus = "";
    int mMultiple, mMut_num, mIs_charsing, mCharsing_num;

    private Integer mOrderId;
    private int mType = 0;
    private int mTotalAmount, mMyMoney;
    private boolean isClick = false;

    private int is_open = 1;
    private int is_follow = 1;
    private double safe_rate = 0.00;
    private String follow_intro;
    private double order_amount = 0.00;
    String share, percentage, subscribe, guarantee, declaration, desc, finish_time, min_reward, max_reward;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setGravity(Gravity.BOTTOM); //显示在底部
        setContentView(R.layout.dialog_pay);
        ButterKnife.bind(this);
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        initInjector();
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("订单确认中，请稍等......");//3.设置显示内容
        progressDialog.setCancelable(false);//4.设置可否用back键关闭对话框
        mPresenter.doGetUserInfo();
        mLottoData = this.getIntent().getStringExtra("data");
        mBetList = this.getIntent().getStringExtra("bet_list");
        mIssus = this.getIntent().getStringExtra("issue");
        mType = this.getIntent().getIntExtra("order_type", 1);

        mMultiple = this.getIntent().getIntExtra("multiple", 1);
        mMut_num = this.getIntent().getIntExtra("mut_num", 1);
        mOrderId = this.getIntent().getIntExtra("order_id", 0);
        mIs_charsing = this.getIntent().getIntExtra("is_charsing", 2);
        mCharsing_num = this.getIntent().getIntExtra("charsing_num", 1);
        mTotalAmount = this.getIntent().getIntExtra("total_amount", 0);
        mMyMoney = this.getIntent().getIntExtra("my_money", 0);

        share = this.getIntent().getStringExtra("share");
        percentage = this.getIntent().getStringExtra("percentage");
        subscribe = this.getIntent().getStringExtra("subscribe");
        guarantee = this.getIntent().getStringExtra("guarantee");
        declaration = this.getIntent().getStringExtra("declaration");
        desc = this.getIntent().getStringExtra("desc");
        finish_time = this.getIntent().getStringExtra("finish_time");
        min_reward = this.getIntent().getStringExtra("min_reward");
        max_reward = this.getIntent().getStringExtra("max_reward");

        is_follow=this.getIntent().getIntExtra("is_follow",1);
        is_open=this.getIntent().getIntExtra("is_open",1);
        safe_rate=this.getIntent().getDoubleExtra("safe_rate",0.00);
        follow_intro=this.getIntent().getStringExtra("follow_intro");

    }

    @OnClick({R.id.btn_pay_dialog_close, R.id.view_pay_dialog_yue, R.id.btn_pay_dialog_confrim})
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.btn_pay_dialog_close:
                finish();
                break;
            case R.id.view_pay_dialog_yue:
                break;

            case R.id.btn_pay_dialog_confrim:

                switch (mType) {
                    case 0:

                        break;
                    case 1:
                        if (NoFastClickUtils.isFastClick()) {
                            showMsg("请等待当前操作完成！");
                        } else {

//                            Integer type, Integer multiple, String bet_code, String issue,
//                                    Integer is_follow, Integer is_charsing, Integer charsing_num,
//                                    Integer mut_num, Integer order_amount,
//                                    String min_reward,String max_reward, Integer is_open,
//                            double safe_rate, String follow_intro) {
                            mPresenter.doCreateOrder(mType, mMultiple, mLottoData, mIssus, mIs_charsing, mCharsing_num, mMut_num, mTotalAmount, min_reward, max_reward,is_follow,is_open,safe_rate,follow_intro);
                            if (progressDialog != null) {
                                progressDialog.show();
                            }
                        }
                        break;
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                    case 10:
                    case 11:
                    case 12:
                    case 13:
                    case 14:
                    case 15:
                    case 16:
                    case 17:
                        if (NoFastClickUtils.isFastClick()) {
                            showMsg("请等待当前操作完成！");
                        } else {

                            if (TextUtils.isEmpty(mLottoData) && TextUtils.isEmpty(mIssus)) {
                                T.showShort("请重新选择！");
                                finish();
                            }
                            if (!TextUtils.isEmpty(share)) {
                                mPresenter.createCopurch(mType, mMultiple, mMut_num, mLottoData, mIssus, String.valueOf(mTotalAmount), share,
                                        percentage, subscribe, guarantee, declaration, desc, finish_time);
                            } else {
                                if (TextUtils.isEmpty(mBetList)) {
                                    mPresenter.doCreateOrder(mType, mMultiple, mLottoData, mIssus, 2, 1, mMut_num, mTotalAmount, min_reward, max_reward,is_follow,is_open,safe_rate,follow_intro);
                                } else {
                                    mPresenter.orderOptimiz(mTotalAmount, mType, mIssus, mLottoData, mBetList, min_reward, max_reward);
                                }
                            }
                            if (progressDialog != null) {
                                progressDialog.show();
                            }
                        }
                        break;

                    case 111:
                        if (NoFastClickUtils.isFastClick()) {
                            showMsg("请等待当前操作完成！");
                        } else {
                            mPresenter.followOrder(mOrderId, mMultiple, mTotalAmount);
                            if (progressDialog != null) {
                                progressDialog.show();
                            }
                        }
                        break;
                }

                break;
        }
    }

    protected void initInjector() {
        DaggerPayComponent.builder().payPresenterModule(new PayPresenterModule(this)).build().inject(this);
    }

    @Override
    public void showUserInfoSuccess(UserInfoBo userInfoBo) {

//            mTvPayPrize.setText(Html.fromHtml("订单金额<font color=\"#D83200\">" + (2 * mMut_num * mMultiple) + "元</font>"));

        int totalAmount = mMyMoney > 0 ? mMyMoney : mTotalAmount;
        mTvPayPrize.setText(Html.fromHtml("订单金额<font color=\"#D83200\">" + totalAmount + "元</font>"));

        if (userInfoBo.getBalance_amount() <= 0 || userInfoBo.getBalance_amount() < totalAmount) {
            T.showShort("余额不足!");
            Intent intent
                    = new Intent(this, RechargeActivity.class);
            startActivity(intent);
            finish();
        }
        if (userInfoBo != null)
            mTvPayBanlance.setText(Html.fromHtml("当前余额<font color=\"#D83200\">" + userInfoBo.getBalance_amount() + "元</font>"));
    }

    @Override
    public void showUserInfoError(String msg) {

    }

    @Override
    public void showBillingSuccess() {

    }

    @Override
    public void showCreateOrderStart() {

    }


    @Override
    public void showCreateOrderSuccess(String luckyHistoryBo) {

        T.showShort("投注成功！");
        Intent intent = new Intent(this, OrderActivity.class);
        startActivity(intent);
        finish();

    }

    @Override
    public void showFollowOrderSuccess(String msg) {
        T.showShort("跟单成功！");
        finish();
    }

    @Override
    public void showError(String ex) {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
        T.showShort(ex);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    void dismissDialog() {


    }

    @Override
    public void showErrorMsg(CharSequence msg) {

    }

    @Override
    public void showErrorMsg(int msgId) {

    }

    @Override
    public void showMsg(CharSequence msg) {

    }

    @Override
    public void showMsg(int msgId) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void showLoading(Object cancel) {

    }

    @Override
    public void dismissLoading() {

    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return null;
    }
}
